package hlhj.fhp.tvlib.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shuyu.gsyvideoplayer.GSYVideoManager
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.activitys.CastDetailAty
import hlhj.fhp.tvlib.adapter.HomeCastNewFgmAdp
import hlhj.fhp.tvlib.javabean.CastListNewBean
import kotlinx.android.synthetic.main.fgm_home_new_cast.*
import kotlinx.android.synthetic.main.fgm_home_new_cast.view.*

/**
 * Created by Never Fear   on 2018\9\11 0011.
Never More....
 */
class HomeCastNew :Fragment(), HomeCastNewFgmAdp.OnItemPlay, SpringView.OnFreshListener {

    private lateinit var rootView:View
    private lateinit var adp:HomeCastNewFgmAdp
    private val hashMap=HashMap<Int,Boolean>()
    private var page=1
    private var head=""
    companion object {
        var tvTitle="广播"
        val datas=ArrayList<CastListNewBean.DataBean.ListBean>()
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.fgm_home_new_cast,null)
        adp= HomeCastNewFgmAdp(datas,hashMap)
        adp.listener=this

        rootView.spView.footer=DefaultFooter(context)
        rootView.spView.header=DefaultHeader(context)
        rootView.spView.setListener(this)
        rootView.recyclerview.layoutManager=LinearLayoutManager(context!!,LinearLayoutManager.VERTICAL,false)
        rootView.recyclerview.adapter=adp
        getData()
        setListener()
        return rootView
    }
    private  fun setListener(){
        adp
                .setOnItemClickListener { adapter, view, position ->
                    val intent = Intent(context!!, CastDetailAty::class.java)
                    intent.putExtra("url",datas[position].radio_source)
                    intent.putExtra("thumb",datas[position].radio_thumb)
                    intent.putExtra("tittle",datas[position].radio_name)
                    intent.putExtra("p",position)
                    intent.putExtra("head",head)
                    startActivity(intent)
                }
    }

    override fun play(p: Int) {
        gsyPlayer.release()
        gsyPlayer.setUp(datas[p].radio_source,false,"")
        gsyPlayer.startButton.performClick()
    }

    override fun onPause() {
        super.onPause()
        rootView.gsyPlayer.onVideoPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        rootView.gsyPlayer.release()
    }
    private fun getData() {
        OkGo.get<String>(Contents.BaseUrl+Contents.CastListNew)
                .params("page",page)
                .execute(object :StringCallback(){
                    override fun onSuccess(response: Response<String>?) {
                        rootView.spView.onFinishFreshAndLoad()
                        val body = response?.body()
                        val listNewBean = Gson().fromJson(body, CastListNewBean::class.java)
                        if (listNewBean.code==1){
                            if (page==1){
                                datas.clear()
                            }
                            datas.addAll(listNewBean.data.list)
                            for (i in 0..datas.size-1){
                                hashMap.put(i,false)
                            }
                            head=listNewBean.data.banner
                            Glide.with(context!!).load(Contents.IMG_URL+listNewBean.data.banner).into(imgHead)
                            tv_tittle.text=listNewBean.data.title
                            adp.notifyDataSetChanged()
                        }
                    }
                    override fun onError(response: Response<String>?) {
                        super.onError(response)

                    }
                }
                )
    }

    override fun onLoadmore() {
        page++
        getData()
    }

    override fun onRefresh() {
        GSYVideoManager.releaseAllVideos()
        page=1
        datas.clear()
        getData()
    }
}